package com.ehotting.edsta.mqbd.testability.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ehotting.eaf.core.utils.OkHttpUtils;
import com.ehotting.eaf.core.utils.ip.IpUtils;
import com.ehotting.edsta.mqbd.testability.service.ITestabilityService;
import com.ehotting.mqbd.api.bean.CommonRequest;
import com.ehotting.mqbd.api.bean.CommonResponse;
import com.ehotting.mqbd.api.bean.solver.SolverInfoBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service
public class TestabilityServiceImpl implements ITestabilityService {


    @Override
    public CommonResponse rePostService(CommonRequest request, SolverInfoBean solverInfo, String authorization) {
        OkHttpUtils okHttpUtils = packOkHttpUtils(request, solverInfo, authorization);
        log.info("into rePostService>>>>>");
        String sync = null;
        Object requestBody = request.getRequestBody();
        String requestMethod = request.getRequestMethod();
        if ("post".equalsIgnoreCase(requestMethod)) {
            sync = okHttpUtils.addBody(requestBody).postBody(true).sync();
        }else if ("get".equalsIgnoreCase(requestMethod)){
            String string = JSON.toJSONString(requestBody, SerializerFeature.DisableCircularReferenceDetect);
            Map<String, String> map = JSON.parseObject(string, Map.class);
            sync = okHttpUtils.addParam(map).get().sync();
        }else if ("delete".equalsIgnoreCase(requestMethod)){
            sync = okHttpUtils.addBody(requestBody).delete(true).sync();
        }
        Map<String, Object> webInfo = request.getWebInfo();
        return packCommonResponse(sync, webInfo);
    }

    private CommonResponse packCommonResponse(String sync, Map<String, Object> webInfo) {
        Map<String,Object> customResponse = null;
        if (sync != null) {
            customResponse = JSON.parseObject(sync, Map.class);
        }
        CommonResponse commonResponse = new CommonResponse();
        commonResponse.setWebInfo(webInfo);
        commonResponse.setResponse(customResponse);
        return commonResponse;
    }

    private OkHttpUtils packOkHttpUtils(CommonRequest request, SolverInfoBean solverInfo, String authorization) {
        String newUrl = "http://"+solverInfo.toStringIpAndPort()+ "/api" +request.getRequestUrl();
        log.info("requestUrl:{}",newUrl);
        return OkHttpUtils.builder().url(newUrl)
                .addHeader(request.getHeaders())
                .addHeader("authorization", authorization);
    }

    @Override
    public CommonResponse rePostUploadService(CommonRequest request, SolverInfoBean solverInfo, String authorization) {
        log.info("into rePostUploadService>>>>>");
        OkHttpUtils okHttpUtils = packOkHttpUtils(request, solverInfo, authorization);

        String sync;
        String string = JSON.toJSONString(request.getRequestBody(), SerializerFeature.DisableCircularReferenceDetect);
        Map<String, String> map = JSON.parseObject(string, Map.class);
        sync = okHttpUtils.addParam(map).uploadFileByDp();
        Map<String, Object> webInfo = request.getWebInfo();
        return packCommonResponse(sync, webInfo);
    }


}
